package com.zxy.allSearcher.job.once;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zxy.allSearcher.model.entity.Post;
import com.zxy.allSearcher.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 爬取测试类
 *
 * @author zxy
 * @since 2023/9/15
 **/
// @Component
@Slf4j
public class FetchInitPostList implements CommandLineRunner {
    @Resource
    private PostService postService;

    // 爬取数据测试

    @Override
    public void run(String... args) throws Exception {
        // 1、获取数据
        String json = "{\"current\": 1, \"pageSize\": 8, \"sortField\": \"createTime\", \"sortOrder\": \"descend\", \"category\": \"文章\"}";
        String url = "https://www.code-nav.cn/api/post/search/page/vo";

        String result = HttpRequest
                .post(url)
                .body(json)
                .execute()
                .body();

        System.out.println(result);
        Map<String, Object> map = JSONUtil.toBean(result, Map.class);
        JSONObject data = (JSONObject) map.get("data"); // 默认返回Object对象，不利于提取数据，JSONObject 可用于提前字段属性
        JSONArray records = (JSONArray) data.get("records");

        // 2、数据转换
        // 考虑不同数据特殊性（json语法、makedom语法，图片防盗链）
        List<Post> postList = new ArrayList<>();
        // 将数据转换为我们需要的，可替换成流式计算
        for (Object record : records) {
            JSONObject tempRecord = (JSONObject) record;

            Post post = new Post();
            // 数据类型明确，getStr;
            post.setTitle(tempRecord.getStr("title"));
            post.setContent(tempRecord.getStr("content"));
            JSONArray tags = (JSONArray) tempRecord.get("tags"); // 数据复杂
            List<String> tagList = tags.toList(String.class);
            post.setTags(JSONUtil.toJsonStr(tagList));
            post.setUserId(1L);

            postList.add(post);
        }
        // 3、数据入库，略
        postService.saveBatch(postList);

        System.out.println(records);
    }
}
